home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / filladapt.el < prev    next >
Encoding:
Text File  |  1995-08-30  |  25.2 KB  |  765 lines

  1. ;;; filladapt.el --- adaptive fill; replacement for fill commands
  2.  
  3. ;; Keywords: wp
  4.  
  5. ;;; Copyright (C) 1989, 1995 Kyle E. Jones
  6. ;;;
  7. ;;; This program is free software; you can redistribute it and/or modify
  8. ;;; it under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 2, or (at your option)
  10. ;;; any later version.
  11. ;;;
  12. ;;; This program is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; A copy of the GNU General Public License can be obtained from this
  18. ;;; program's author (send electronic mail to kyle@uunet.uu.net) or from
  19. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  20. ;;; 02139, USA.
  21. ;;;
  22. ;;; Send bug reports to kyle@wonderworks.com
  23.  
  24. ;; LCD Archive Entry: 
  25. ;; filladapt|Kyle Jones|kyle_jones@wonderworks.com| 
  26. ;; Minor mode to adaptively set fill-prefix and overload filling functions|
  27. ;; 18-August-1995|2.07|~/packages/filladapt.el| 
  28.  
  29. ;; These functions enhance the default behavior of Emacs' Auto Fill
  30. ;; mode and the commands fill-paragraph, lisp-fill-paragraph and
  31. ;; fill-region-as-paragraph.
  32. ;;
  33. ;; The chief improvement is that the beginning of a line to be
  34. ;; filled is examined and, based on information gathered, an
  35. ;; appropriate value for fill-prefix is constructed.  Also the
  36. ;; boundaries of the current paragraph are located.  This occurs
  37. ;; only if the fill prefix is not already non-nil.
  38. ;;
  39. ;; The net result of this is that blurbs of text that are offset
  40. ;; from left margin by asterisks, dashes, and/or spaces, numbered
  41. ;; examples, included text from USENET news articles, etc. are
  42. ;; generally filled correctly with no fuss.
  43. ;;
  44. ;; Since this package replaces existing Emacs functions, it cannot
  45. ;; be autoloaded.  Save this in a file named filladapt.el in a
  46. ;; Lisp directory that Emacs knows about, byte-compile it and put
  47. ;;    (require 'filladapt)
  48. ;; in your .emacs file.
  49. ;;
  50. ;; Note that in this release Filladapt mode is a minor mode and it is
  51. ;; _off_ by default.  If you want it to be on by default, use
  52. ;;   (setq-default filladapt-mode t)
  53. ;;
  54. ;; M-x filladapt-mode toggles Filladapt mode on/off in the current
  55. ;; buffer.
  56. ;;
  57. ;; Use
  58. ;;     (add-hook 'text-mode-hook 'turn-on-filladapt-mode)
  59. ;; to have Filladapt always enabled in Text mode.
  60. ;;
  61. ;; Use
  62. ;;     (add-hook 'c-mode-hook 'turn-off-filladapt-mode)
  63. ;; to have Filladapt always disabled in C mode.
  64. ;;
  65. ;; In many cases, you can extend Filladapt by adding appropriate
  66. ;; entries to the following three `defvar's.  See `postscript-comment'
  67. ;; or `texinfo-comment' as a sample of what needs to be done.
  68. ;;
  69. ;;     filladapt-token-table
  70. ;;     filladapt-token-match-table
  71. ;;     filladapt-token-conversion-table
  72.  
  73. (provide 'filladapt)
  74.  
  75. (defvar filladapt-version "2.07"
  76.   "Version string for filladapt.")
  77.  
  78. (defvar filladapt-mode nil
  79.   "*Non-nil means that Filladapt minor mode is enabled.
  80. Use the filladapt-mode command to toggle the mode on/off.")
  81. (make-variable-buffer-local 'filladapt-mode)
  82.  
  83. (defvar filladapt-mode-line-string " Filladapt"
  84.   "*String to display in the modeline when Filladapt mode is active.
  85. Set this to nil if you don't want a modeline indicator for Filladapt.")
  86.  
  87. ;; install on minor-mode-alist
  88. (or (assq 'filladapt-mode minor-mode-alist)
  89.     (setq minor-mode-alist (cons (list 'filladapt-mode
  90.                        'filladapt-mode-line-string)
  91.                  minor-mode-alist)))
  92.  
  93. (defvar filladapt-token-table
  94.   '(
  95.     ;; Included text in news or mail replies
  96.     (">+" . citation->)
  97.     ;; Included text generated by SUPERCITE.  We can't hope to match all
  98.     ;; the possible variations, your mileage may vary.
  99.     ("[A-Za-z0-9][^'`\"< \t\n]*>[ \t]*" . supercite-citation)
  100.     ;; Lisp comments
  101.     (";+" . lisp-comment)
  102.     ;; UNIX shell comments
  103.     ("#+" . sh-comment)
  104.     ;; Postscript comments
  105.     ("%+" . postscript-comment)
  106.     ;; C++ comments
  107.     ("///*" . c++-comment)
  108.     ;; Texinfo comments
  109.     ("@c[ \t]" . texinfo-comment)
  110.     ("@comment[ \t]" . texinfo-comment)
  111.     ;; Bullet types.
  112.     ;;
  113.     ;; 1. xxxxx
  114.     ;;    xxxxx
  115.     ;;
  116.     ("[0-9]+\\.[ \t]" . bullet)
  117.     ;;
  118.     ;; 2.1.3  xxxxx xx x xx x
  119.     ;;        xxx
  120.     ;;
  121.     ("[0-9]+\\(\\.[0-9]+\\)+[ \t]" . bullet)
  122.     ;;
  123.     ;; a. xxxxxx xx
  124.     ;;    xxx xxx
  125.     ;;
  126.     ("[A-Za-z]\\.[ \t]" . bullet)
  127.     ;;
  128.     ;; 1) xxxx x xx x xx   or   (1) xx xx x x xx xx
  129.     ;;    xx xx xxxx                xxx xx x x xx x
  130.     ;;
  131.     ("(?[0-9]+)[ \t]" . bullet)
  132.     ;;
  133.     ;; a) xxxx x xx x xx   or   (a) xx xx x x xx xx
  134.     ;;    xx xx xxxx                xxx xx x x xx x
  135.     ;;
  136.     ("(?[A-Za-z])[ \t]" . bullet)
  137.     ;;
  138.     ;; 2a. xx x xxx x x xxx
  139.     ;;     xxx xx x xx x
  140.     ;;
  141.     ("[0-9]+[A-Za-z]\\.[ \t]" . bullet)
  142.     ;;
  143.     ;; 1a) xxxx x xx x xx   or   (1a) xx xx x x xx xx
  144.     ;;     xx xx xxxx                 xxx xx x x xx x
  145.     ;;
  146.     ("(?[0-9]+[A-Za-z])[ \t]" . bullet)
  147.     ;;
  148.     ;; -  xx xxx xxxx   or   *  xx xx x xxx xxx
  149.     ;;    xxx xx xx             x xxx x xx x x x
  150.     ;;
  151.     ("[-~*+]+[ \t]" . bullet)
  152.     ;;
  153.     ;; o  xx xxx xxxx xx x xx xxx x xxx xx x xxx
  154.     ;;    xxx xx xx 
  155.     ;;
  156.     ("o[ \t]" . bullet)
  157.     ;; don't touch
  158.     ("[ \t]+" . space)
  159.     ("$" . end-of-line)
  160.    )
  161.   "Table of tokens filladapt knows about.
  162. Format is
  163.  
  164.    ((REGEXP . SYM) ...)
  165.  
  166. filladapt uses this table to build a tokenized representation of
  167. the beginning of the current line.  Each REGEXP is matched
  168. against the beginning of the line until a match is found.
  169. Matching is done case-sensitively.  The corresponding SYM is
  170. added to the list, point is moved to (match-end 0) and the
  171. process is repeated.  The process ends when there is no REGEXP in
  172. the table that matches what is at point.")
  173.  
  174. (defvar filladapt-not-token-table
  175.   '(
  176.     "[Ee].g."
  177.     "[Ii].e."
  178.     ;; end-of-line isn't a token if whole line is empty
  179.     "^$"
  180.    )
  181.   "List of regexps that can never be a token.
  182. Before trying the regular expressions in filladapt-token-table,
  183. the regexps in this list are tried.  If any regexp in this list
  184. matches what is at point then the token generator gives up and
  185. doesn't try any of the regexps in filladapt-token-table.
  186.  
  187. Regexp matching is done case-sensitively.")
  188.  
  189. (defvar filladapt-token-match-table
  190.   '(
  191.     (citation-> citation->)
  192.     (supercite-citation supercite-citation)
  193.     (lisp-comment lisp-comment)
  194.     (sh-comment sh-comment)
  195.     (postscript-comment postscript-comment)
  196.     (c++-comment c++-comment)
  197.     (texinfo-comment texinfo-comment)
  198.     (bullet)
  199.     (space bullet space)
  200.    )
  201.   "Table describing what tokens a certain token will match.
  202.  
  203. To decide whether a line belongs in the current paragraph,
  204. filladapt creates a token list for the fill prefix of both lines.
  205. Tokens and the columns where tokens end are compared.  This table
  206. specifies what a certain token will match.
  207.  
  208. Table format is
  209.  
  210.    (SYM [SYM1 [SYM2 ...]])
  211.  
  212. The first symbol SYM is the token, subsequent symbols are the
  213. tokens that SYM will match.")
  214.  
  215. (defvar filladapt-token-match-many-table
  216.   '(
  217.     space
  218.    )
  219.   "List of tokens that can match multiple tokens.
  220. If one of these tokens appears in a token list, it will eat all
  221. matching tokens in a token list being matched against it until it
  222. encounters a token that doesn't match or a token that ends on
  223. a greater column number.")
  224.  
  225. (defvar filladapt-token-paragraph-start-table
  226.   '(
  227.     bullet
  228.    )
  229.   "List of tokens that indicate the start of a paragraph.
  230. If parsing a line generates a token list containing one of
  231. these tokens, then the line is considered to be the start of a
  232. paragraph.")
  233.  
  234. (defvar filladapt-token-conversion-table
  235.   '(
  236.     (citation-> . exact)
  237.     (supercite-citation . exact)
  238.     (lisp-comment . exact)
  239.     (sh-comment . exact)
  240.     (postscript-comment . exact)
  241.     (c++-comment . exact)
  242.     (texinfo-comment . exact)
  243.     (bullet . spaces)
  244.     (space . exact)
  245.     (end-of-line . exact)
  246.    )
  247.   "Table that specifies how to convert a token into a fill prefix.
  248. Table format is
  249.  
  250.    ((SYM . HOWTO) ...)
  251.  
  252. SYM is the symbol naming the token to be converted.
  253. HOWTO specifies how to do the conversion.
  254.   `exact' means copy the token's string directly into the fill prefix.
  255.   `spaces' means convert all characters in the token string that are
  256.       not a TAB or a space into spaces and copy the resulting string into 
  257.       the fill prefix.")
  258.  
  259. (defvar filladapt-function-table
  260.   (let ((assoc-list
  261.      (list (cons 'fill-paragraph (symbol-function 'fill-paragraph))
  262.            (cons 'fill-region-as-paragraph
  263.              (symbol-function 'fill-region-as-paragraph))
  264.            (cons 'do-auto-fill (symbol-function 'do-auto-fill)))))
  265.     ;; v18 Emacs doesn't have lisp-fill-paragraph
  266.     (if (fboundp 'lisp-fill-paragraph)
  267.     (nconc assoc-list
  268.            (list (cons 'lisp-fill-paragraph
  269.                (symbol-function 'lisp-fill-paragraph)))))
  270.     assoc-list )
  271.   "Table containing the old function definitions that filladapt usurps.")
  272.  
  273. (defvar filladapt-fill-paragraph-post-hook nil
  274.   "Hooks run after filladapt runs fill-paragraph.")
  275.  
  276. (defvar filladapt-inside-filladapt nil
  277.   "Non-nil if the filladapt version of a fill function executing.
  278. Currently this is only checked by the filladapt version of
  279. fill-region-as-paragraph to avoid this infinite recursion:
  280.  
  281.   fill-region-as-paragraph -> fill-paragraph -> fill-region-as-paragraph ...")
  282.  
  283. (defvar filladapt-debug nil
  284.   "Non-nil means filladapt debugging is enabled.
  285. Use the filladapt-debug command to turn on debugging.
  286.  
  287. With debugging enabled, filladapt will
  288.  
  289.     a. display the proposed indentation with the tokens highlighted
  290.        using filladapt-debug-indentation-face-1 and
  291.        filladapt-debug-indentation-face-2.
  292.     b. display the current paragraph using the face specified by
  293.        filladapt-debug-paragraph-face.")
  294.  
  295. (if filladapt-debug
  296.     (add-hook 'post-command-hook 'filladapt-display-debug-info-maybe))
  297.  
  298. (defvar filladapt-debug-indentation-face-1 'highlight
  299.   "Face used to display the indentation when debugging is enabled.")
  300.  
  301. (defvar filladapt-debug-indentation-face-2 'secondary-selection
  302.   "Another face used to display the indentation when debugging is enabled.")
  303.  
  304. (defvar filladapt-debug-paragraph-face 'bold
  305.   "Face used to display the current paragraph when debugging is enabled.")
  306.  
  307. (defvar filladapt-debug-indentation-extents nil)
  308. (make-variable-buffer-local 'filladapt-debug-indentation-extents)
  309. (defvar filladapt-debug-paragraph-extent nil)
  310. (make-variable-buffer-local 'filladapt-debug-paragraph-extent)
  311.  
  312. (defun do-auto-fill ()
  313.   (catch 'done
  314.     (if (and filladapt-mode (null fill-prefix))
  315.     (save-restriction
  316.       (let ((paragraph-ignore-fill-prefix nil)
  317.         ;; if the user wanted this stuff, they probably
  318.         ;; wouldn't be using filladapt-mode.
  319.         (adaptive-fill-mode nil)
  320.         (adaptive-fill-regexp nil)
  321.         ;; need this or Emacs 19 ignores fill-prefix when
  322.         ;; inside a comment.
  323.         (comment-multi-line t)
  324.         (filladapt-inside-filladapt t)
  325.         fill-prefix retval)
  326.         (if (filladapt-adapt nil nil)
  327.         (progn
  328.           (setq retval (filladapt-funcall 'do-auto-fill))
  329.           (throw 'done retval))))))
  330.     (filladapt-funcall 'do-auto-fill)))
  331.  
  332. (defun filladapt-fill-paragraph (function arg)
  333.   (catch 'done
  334.     (if (and filladapt-mode (null fill-prefix))
  335.     (save-restriction
  336.       (let ((paragraph-ignore-fill-prefix nil)
  337.         ;; if the user wanted this stuff, they probably
  338.         ;; wouldn't be using filladapt-mode.
  339.         (adaptive-fill-mode nil)
  340.         (adaptive-fill-regexp nil)
  341.         ;; need this or Emacs 19 ignores fill-prefix when
  342.         ;; inside a comment.
  343.         (comment-multi-line t)
  344.         fill-prefix retval)
  345.         (if (filladapt-adapt t nil)
  346.         (progn
  347.           (setq retval (filladapt-funcall function arg))
  348.           (run-hooks 'filladapt-fill-paragraph-post-hook)
  349.           (throw 'done retval))))))
  350.     ;; filladapt-adapt failed, so do fill-paragraph normally.
  351.     (filladapt-funcall function arg)))
  352.  
  353. (defun fill-paragraph (arg)
  354.   (interactive "*P")
  355.   (let ((filladapt-inside-filladapt t))
  356.     (filladapt-fill-paragraph 'fill-paragraph arg)))
  357.  
  358. (defun lisp-fill-paragraph (arg)
  359.   (interactive "*P")
  360.   (let ((filladapt-inside-filladapt t))
  361.     (filladapt-fill-paragraph 'lisp-fill-paragraph arg)))
  362.  
  363. (defun fill-region-as-paragraph (beg end &optional justify nosqueeze)
  364.   (interactive "*r\nP")
  365.   (if (and filladapt-mode (not filladapt-inside-filladapt))
  366.       (save-restriction
  367.     (narrow-to-region beg end)
  368.     (let ((filladapt-inside-filladapt t)
  369.           line-start last-token)
  370.       (goto-char beg)
  371.       (end-of-line)
  372.       (while (zerop (forward-line))
  373.         (if (setq last-token
  374.               (car (filladapt-tail (filladapt-parse-prefixes))))
  375.         (progn
  376.           (setq line-start (point))
  377.           (move-to-column (nth 1 last-token))
  378.           (delete-region line-start (point))))
  379.         ;; Dance...
  380.         ;;
  381.         ;; Do this instead of (delete-char -1) to keep
  382.         ;; markers on the correct side of the whitespace.
  383.         (goto-char (1- (point)))
  384.         (insert " ")
  385.         (delete-char 1)
  386.  
  387.         (end-of-line))
  388.       (goto-char beg)
  389.       (fill-paragraph justify))
  390.     ;; In XEmacs 19.12 and Emacs 18.59 fill-region relies on
  391.     ;; fill-region-as-paragraph to do this.  If we don't do
  392.     ;; it, fill-region will spin in an endless loop.
  393.     (goto-char (point-max)))
  394.     (condition-case nil
  395.     ;; four args for Emacs 19.29
  396.     (filladapt-funcall 'fill-region-as-paragraph beg end justify nosqueeze)
  397.       ;; three args for the rest of the world.
  398.       (wrong-number-of-arguments
  399.     (filladapt-funcall 'fill-region-as-paragraph beg end justify)))))
  400.  
  401. (defvar zmacs-region-stays) ; for XEmacs
  402.  
  403. (defun filladapt-mode (&optional arg)
  404.   "Toggle Filladapt minor mode.
  405. With arg, turn Filladapt mode on iff arg is positive.  When
  406. Filladapt mode is enabled, auto-fill-mode and the fill-paragraph
  407. command are both smarter about guessing a proper fill-prefix and
  408. finding paragraph boundaries when bulleted and indented lines and
  409. paragraphs are used."
  410.   (interactive "P")
  411.   ;; don't deactivate the region.
  412.   (setq zmacs-region-stays t)
  413.   (setq filladapt-mode (or (and arg (> (prefix-numeric-value arg) 0))
  414.                (and (null arg) (null filladapt-mode))))
  415.   (if (fboundp 'force-mode-line-update)
  416.       (force-mode-line-update)
  417.     (set-buffer-modified-p (buffer-modified-p))))
  418.  
  419. (defun turn-on-filladapt-mode ()
  420.   "Unconditionally turn on Filladapt mode in the current buffer."
  421.   (filladapt-mode 1))
  422.  
  423. (defun turn-off-filladapt-mode ()
  424.   "Unconditionally turn off Filladapt mode in the current buffer."
  425.   (filladapt-mode -1))
  426.  
  427. (defun filladapt-funcall (function &rest args)
  428.   "Call the old definition of a function that filladapt has usurped."
  429.   (apply (cdr (assoc function filladapt-function-table)) args))
  430.  
  431. (defun filladapt-paragraph-start (list)
  432.   "Returns non-nil if LIST contains a paragraph starting token.
  433. LIST should be a token list as returned by filladapt-parse-prefixes."
  434.   (catch 'done
  435.     (while list
  436.       (if (memq (car (car list)) filladapt-token-paragraph-start-table)
  437.       (throw 'done t))
  438.       (setq list (cdr list)))))
  439.  
  440. (defun filladapt-parse-prefixes ()
  441.   "Parse all the tokens after point and return a list of them.
  442. The tokens regular expressions are specified in
  443. filladapt-token-table.  The list returned is of this form
  444.  
  445.   ((SYM COL STRING) ...)
  446.  
  447. SYM is a token symbol as found in filladapt-token-table.
  448. COL is the column at which the token ended.
  449. STRING is the token's text."
  450.   (save-excursion
  451.     (let ((token-list nil)
  452.       (done nil)
  453.       (old-point (point))
  454.       (case-fold-search nil)
  455.       token-table not-token-table)
  456.       (catch 'done
  457.     (while (not done)
  458.       (setq not-token-table filladapt-not-token-table)
  459.       (while not-token-table
  460.         (if (looking-at (car not-token-table))
  461.         (throw 'done t))
  462.         (setq not-token-table (cdr not-token-table)))
  463.       (setq token-table filladapt-token-table
  464.         done t)
  465.       (while token-table
  466.         (if (null (looking-at (car (car token-table))))
  467.         (setq token-table (cdr token-table))
  468.           (goto-char (match-end 0))
  469.           (setq token-list (cons (list (cdr (car token-table))
  470.                        (current-column)
  471.                        (buffer-substring
  472.                         (match-beginning 0)
  473.                         (match-end 0)))
  474.                      token-list)
  475.             token-table nil
  476.             done (eq (point) old-point)
  477.             old-point (point))))))
  478.       (nreverse token-list))))
  479.  
  480. (defun filladapt-tokens-match-p (list1 list2)
  481.   "Compare two token lists and return non-nil if they match, nil otherwise.
  482. The lists are walked through in lockstep, comparing tokens.
  483.  
  484. When two tokens A and B are compared, they are considered to
  485. match if
  486.  
  487.     1. A appears in B's list of matching tokens or
  488.        B appears in A's list of matching tokens
  489. and
  490.     2. A and B both end at the same column
  491.          or
  492.        A can match multiple tokens and ends at a column > than B
  493.          or
  494.        B can match multiple tokens and ends at a column > than A
  495.  
  496. In the case where the end columns differ the list pointer for the
  497. token with the greater end column is not moved forward, which allows
  498. its current token to be matched the next token in the other list in
  499. the next iteration of the matching loop.
  500.  
  501. All tokens must be matched in order for the lists to be consiered
  502. matching."
  503.   (let ((matched t)
  504.     (done nil))
  505.     (while (and (not done) list1 list2)
  506.       (let* ((token1 (car (car list1)))
  507.          (token1-matches-many-p
  508.              (memq token1 filladapt-token-match-many-table))
  509.          (token1-matches (cdr (assq token1 filladapt-token-match-table)))
  510.          (token1-endcol (nth 1 (car list1)))
  511.          (token2 (car (car list2)))
  512.          (token2-matches-many-p
  513.              (memq token2 filladapt-token-match-many-table))
  514.          (token2-matches (cdr (assq token2 filladapt-token-match-table)))
  515.          (token2-endcol (nth 1 (car list2)))
  516.          (tokens-match (or (memq token1 token2-matches)
  517.                    (memq token2 token1-matches))))
  518.     (cond ((not tokens-match)
  519.            (setq matched nil
  520.              done t))
  521.           ((and token1-matches-many-p token2-matches-many-p)
  522.            (cond ((= token1-endcol token2-endcol)
  523.               (setq list1 (cdr list1)
  524.                 list2 (cdr list2)))
  525.              ((< token1-endcol token2-endcol)
  526.               (setq list1 (cdr list1)))
  527.              (t
  528.               (setq list2 (cdr list2)))))
  529.           (token1-matches-many-p
  530.            (cond ((= token1-endcol token2-endcol)
  531.               (setq list1 (cdr list1)
  532.                 list2 (cdr list2)))
  533.              ((< token1-endcol token2-endcol)
  534.               (setq matched nil
  535.                 done t))
  536.              (t
  537.               (setq list2 (cdr list2)))))
  538.           (token2-matches-many-p
  539.            (cond ((= token1-endcol token2-endcol)
  540.               (setq list1 (cdr list1)
  541.                 list2 (cdr list2)))
  542.              ((< token2-endcol token1-endcol)
  543.               (setq matched nil
  544.                 done t))
  545.              (t
  546.               (setq list1 (cdr list1)))))
  547.           ((= token1-endcol token2-endcol)
  548.            (setq list1 (cdr list1)
  549.              list2 (cdr list2)))
  550.           (t
  551.            (setq matched nil
  552.              done t)))))
  553.     (and matched (null list1) (null list2)) ))
  554.  
  555. (defun filladapt-make-fill-prefix (list)
  556.   "Build a fill-prefix for a token LIST.
  557. filladapt-token-conversion-table specifies how this is done."
  558.   (let ((prefix-list nil)
  559.     (conversion-spec nil))
  560.     (while list
  561.       (setq conversion-spec (cdr (assq (car (car list))
  562.                        filladapt-token-conversion-table)))
  563.       (cond ((eq conversion-spec 'spaces)
  564.          (setq prefix-list
  565.            (cons
  566.             (filladapt-convert-to-spaces (nth 2 (car list)))
  567.             prefix-list)))
  568.         ((eq conversion-spec 'exact)
  569.          (setq prefix-list
  570.            (cons
  571.             (nth 2 (car list))
  572.             prefix-list))))
  573.       (setq list (cdr list)))
  574.     (apply (function concat) (nreverse prefix-list)) ))
  575.  
  576. (defun filladapt-convert-to-spaces (string)
  577.   "Return a copy of STRING, with all non-tabs and non-space changed to spaces."
  578.   (let ((i 0)
  579.     (space-list '(?\  ?\t))
  580.     (space ?\ )
  581.     (lim (length string)))
  582.     (setq string (copy-sequence string))
  583.     (while (< i lim)
  584.       (if (not (memq (aref string i) space-list))
  585.       (aset string i space))
  586.       (setq i (1+ i)))
  587.     string ))
  588.  
  589. (defun filladapt-adapt (paragraph debugging)
  590.   "Set fill-prefix based on the contents of the current line.
  591.  
  592. If the first arg PARAGRAPH is non-nil, also set a clipping region
  593. around the current paragraph.
  594.  
  595. If the second arg DEBUGGING is non-nil, don't do the kludge that's
  596. necessary to make certain paragraph fills work properly."
  597.   (save-excursion
  598.     (beginning-of-line)
  599.     (let ((token-list (filladapt-parse-prefixes))
  600.       curr-list done)
  601.       (if (null token-list)
  602.       nil
  603.     (setq fill-prefix (filladapt-make-fill-prefix token-list))
  604.     (if paragraph
  605.         (let (beg end)
  606.           (if (filladapt-paragraph-start token-list)
  607.           (setq beg (point))
  608.         (save-excursion
  609.           (setq done nil)
  610.           (while (not done)
  611.             (cond ((not (= 0 (forward-line -1)))
  612.                (setq done t
  613.                  beg (point)))
  614.               ((not (filladapt-tokens-match-p
  615.                  token-list
  616.                  (setq curr-list (filladapt-parse-prefixes))))
  617.                (forward-line 1)
  618.                (setq done t
  619.                  beg (point)))
  620.               ((filladapt-paragraph-start curr-list)
  621.                (setq done t
  622.                  beg (point)))))))
  623.           (save-excursion
  624.         (setq done nil)
  625.         (while (not done)
  626.           (cond ((not (= 0 (progn (end-of-line) (forward-line 1))))
  627.              (setq done t
  628.                    end (point)))
  629.             ((not (filladapt-tokens-match-p
  630.                    token-list
  631.                    (setq curr-list (filladapt-parse-prefixes))))
  632.              (setq done t
  633.                    end (point)))
  634.             ((filladapt-paragraph-start curr-list)
  635.              (setq done t
  636.                    end (point))))))
  637.           (narrow-to-region beg end)
  638.           ;; Multiple spaces at the beginning of the first
  639.           ;; line of a hanging list paragraph get squashed by
  640.           ;; fill-paragraph.  We kludge around this by hiding
  641.           ;; the real line begining and creating a fake one
  642.           ;; that's all spaces.  fill-paragraph won't touch a
  643.           ;; line prefix of all spaces so we win.  The post
  644.           ;; hook cleans up the spaces after fill-paragraph
  645.           ;; has been called.
  646.           (if (and paragraph (not debugging))
  647.           (let (col)
  648.             (setq col (nth 1 (car (filladapt-tail token-list))))
  649.             (goto-char (point-min))
  650.             (move-to-column col)
  651.             (narrow-to-region (point) (point-max))
  652.             (insert-char (string-to-char " ") col)
  653.             (add-hook 'filladapt-fill-paragraph-post-hook
  654.                   'filladapt-cleanup-kludge-at-point-min)))))
  655.     t ))))
  656.  
  657. (defun filladapt-cleanup-kludge-at-point-min ()
  658.   "Cleanup the paragraph fill kludge.
  659. See filladapt-adapt."
  660.   (save-excursion
  661.     (goto-char (point-min))
  662.     (while (looking-at " ")
  663.       (delete-char 1))))
  664.  
  665. (defun filladapt-tail (list)
  666.   "Returns the last cons in LIST."
  667.   (if (null list)
  668.       nil
  669.     (while (consp (cdr list))
  670.       (setq list (cdr list)))
  671.     list ))
  672.  
  673. (defun filladapt-delete-extent (e)
  674.   (if (fboundp 'delete-extent)
  675.       (delete-extent e)
  676.     (delete-overlay e)))
  677.  
  678. (defun filladapt-make-extent (beg end)
  679.   (if (fboundp 'make-extent)
  680.       (make-extent beg end)
  681.     (make-overlay beg end)))
  682.  
  683. (defun filladapt-set-extent-endpoints (e beg end)
  684.   (if (fboundp 'set-extent-endpoints)
  685.       (set-extent-endpoints e beg end)
  686.     (move-overlay e beg end)))
  687.  
  688. (defun filladapt-set-extent-property (e prop val)
  689.   (if (fboundp 'set-extent-property)
  690.       (set-extent-property e prop val)
  691.     (overlay-put e prop val)))
  692.  
  693. (defun filladapt-debug ()
  694.   "Toggle filladapt debugging on/off in the current buffer."
  695. ;;  (interactive)
  696.   (make-local-variable 'filladapt-debug)
  697.   (setq filladapt-debug (not filladapt-debug))
  698.   ;; make sure these faces exist at least
  699.   (make-face 'filladapt-debug-indentation-face-1)
  700.   (make-face 'filladapt-debug-indentation-face-2)
  701.   (make-face 'filladapt-debug-paragraph-face)
  702.   (if (null filladapt-debug)
  703.       (progn
  704.     (mapcar (function (lambda (e) (filladapt-set-extent-endpoints e 1 1)))
  705.         filladapt-debug-indentation-extents)
  706.     (if filladapt-debug-paragraph-extent
  707.         (progn
  708.           (filladapt-delete-extent filladapt-debug-paragraph-extent)
  709.           (setq filladapt-debug-paragraph-extent nil)))))
  710.   (add-hook 'post-command-hook 'filladapt-display-debug-info-maybe))
  711.  
  712. (defun filladapt-display-debug-info-maybe ()
  713.   (cond ((null filladapt-debug) nil)
  714.     (fill-prefix nil)
  715.     (t
  716.      (if (null filladapt-debug-paragraph-extent)
  717.          (let ((e (filladapt-make-extent 1 1)))
  718.            (filladapt-set-extent-property e 'detachable nil)
  719.            (filladapt-set-extent-property e 'evaporate nil)
  720.            (filladapt-set-extent-property e 'face
  721.                           filladapt-debug-paragraph-face)
  722.            (setq filladapt-debug-paragraph-extent e)))
  723.      (save-excursion
  724.        (save-restriction
  725.          (let ((ei-list filladapt-debug-indentation-extents)
  726.            (ep filladapt-debug-paragraph-extent)
  727.            (face filladapt-debug-indentation-face-1)
  728.            fill-prefix token-list)
  729.            (if (null (filladapt-adapt t t))
  730.            (progn
  731.              (filladapt-set-extent-endpoints ep 1 1)
  732.              (while ei-list
  733.                (filladapt-set-extent-endpoints (car ei-list) 1 1)
  734.                (setq ei-list (cdr ei-list))))
  735.          (filladapt-set-extent-endpoints ep (point-min) (point-max))
  736.          (beginning-of-line)
  737.          (setq token-list (filladapt-parse-prefixes))
  738.          (message "(%s)" (mapconcat (function
  739.                        (lambda (q) (symbol-name (car q))))
  740.                       token-list
  741.                       " "))
  742.          (while token-list
  743.            (if ei-list
  744.                (setq e (car ei-list)
  745.                  ei-list (cdr ei-list))
  746.              (setq e (filladapt-make-extent 1 1))
  747.              (filladapt-set-extent-property e 'detachable nil)
  748.              (filladapt-set-extent-property e 'evaporate nil)
  749.              (setq filladapt-debug-indentation-extents
  750.                (cons e filladapt-debug-indentation-extents)))
  751.            (filladapt-set-extent-property e 'face face)
  752.            (filladapt-set-extent-endpoints e (point)
  753.                            (progn
  754.                              (move-to-column
  755.                               (nth 1
  756.                                (car token-list)))
  757.                              (point)))
  758.            (if (eq face filladapt-debug-indentation-face-1)
  759.                (setq face filladapt-debug-indentation-face-2)
  760.              (setq face filladapt-debug-indentation-face-1))
  761.            (setq token-list (cdr token-list)))
  762.          (while ei-list
  763.            (filladapt-set-extent-endpoints (car ei-list) 1 1)
  764.            (setq ei-list (cdr ei-list))))))))))
  765.